//
//  BCTitleInputPayPassView.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/12/11.
//  Copyright © 2018 guoshikeji. All rights reserved.
//

#import "BCTitleInputPayPassView.h"

@interface BCTitleInputPayPassView () <QMUITextFieldDelegate>

@property (nonatomic, strong) QMUILabel *passwordDigitsLabel;

@end

@implementation BCTitleInputPayPassView

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.inputTextField.delegate = self;
        self.inputTextField.secureTextEntry = true;
        self.inputTextField.keyboardType = UIKeyboardTypeNumberPad;
        //
        self.passwordDigitsLabel = [QMUILabel new];
        self.passwordDigitsLabel.textColor = [UIColor darkMainColor];
        self.passwordDigitsLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightRegular];
        [self addSubview:self.passwordDigitsLabel];
        [self.passwordDigitsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self.inputTextField);
            make.centerY.mas_equalTo(self.inputTextField);
        }];
        self.passwordDigitsLabel.alpha = 0;
        
        //
        self.numbersOfPassword = 6;
    }
    return self;
}

- (void)setNumbersOfPassword:(NSInteger)numbersOfPassword {
    _numbersOfPassword = numbersOfPassword;
    //
    self.inputTextField.maximumTextLength = numbersOfPassword;
    
    self.passwordDigitsLabel.text = [NSString stringWithFormat:@"%@",@(numbersOfPassword)];
}


- (void)inputTextFieldTextDidChanged:(NSNotification *)noti {
    [super inputTextFieldTextDidChanged:noti];
    //判断当前输入位数
    if (noti.object==self.inputTextField) {
        QMUITextField *textField = self.inputTextField;
        //
        NSInteger last = self.numbersOfPassword-textField.text.length;
        if (last==0) {
            self.passwordDigitsLabel.text = nil;
        }else {
            self.passwordDigitsLabel.text = [NSString stringWithFormat:@"%@",@(last)];
        }
    }
}

#pragma mark - QMUITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    self.passwordDigitsLabel.alpha = 1;
    return true;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    self.passwordDigitsLabel.alpha = 0;
    return true;
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
    self.passwordDigitsLabel.alpha = 0;
}


- (BOOL)isPassInvalidate {
    return self.inputTextField.text.length==self.numbersOfPassword;
}

@end
